/*
 * MapFileDlg.java
 *
 * Created on January 8, 2005, 11:45 PM
 */

package edu.umd.essic.maskbuilder;

import java.awt.geom.*;
import javax.xml.transform.*;
import java.io.*;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author  Inna
 */
public class MapFileDlg extends javax.swing.JDialog {
    public int Precision, Nbytes, Nlon, Nlat, Nlon_init, Nlat_init, MINval, MAXval, DataType, CellSize;
    public float Left_x, Right_x, Upper_y, Lower_y, NewResolution, Threshold;
    public float LeftX_init, UpperY_init, RightX_init, LowerY_init;
    public boolean OnOK = false;
    private boolean shown = false;
    private boolean NotForChange = false;
    public boolean two_parts_domain = false;
    private String err_text = " ";
    private int CaseLon, CaseLat; // CaseLon = 1 for EW, 2 for WE && EE, 3 for WW
                                  // CaseLat = 1 for NS && NN, 2 for SS, 3 for SN(failure)
    ErrorDlg erd;
    
    String[] Segment = {"N","S","E","W"};
    SpinnerNumberModel modelEW = new SpinnerNumberModel(0,0,180,0.25);
    SpinnerNumberModel modelWE = new SpinnerNumberModel(0,0,180,0.25);
    SpinnerNumberModel modelNS = new SpinnerNumberModel(0,0,90,0.25);
    SpinnerNumberModel modelSN = new SpinnerNumberModel(0,0,90,0.25);
    
    /** Creates new form MapFileDlg */
    public MapFileDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setSize(435, 375);
        NS_Combo.addItem(Segment[0]);
        NS_Combo.addItem(Segment[1]);
        SN_Combo.addItem(Segment[0]);
        SN_Combo.addItem(Segment[1]);
        EW_Combo.addItem(Segment[2]);
        EW_Combo.addItem(Segment[3]);
        WE_Combo.addItem(Segment[2]);
        WE_Combo.addItem(Segment[3]);
        erd = new ErrorDlg(this,true);
        erd.setLocationRelativeTo(this);
        erd.setLocation(50,250);
        Threshold = 0;
    }
    
    public void ReadMapData(BufferedReader in) throws IOException
    { 
        Pos p = new Pos();
        String text = in.readLine();
        Left_x = p.ReadFloat(text);
        Right_x = p.ReadFloat(text);
        Upper_y = p.ReadFloat(text);
        Lower_y = p.ReadFloat(text);
        Nlon = p.ReadInt(text);
        Nlat = p.ReadInt(text);
        Threshold = p.ReadInt(text);
        p = null;
        NotForChange = true;
    }
 
     /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        NS_Combo = new javax.swing.JComboBox();
        SN_Combo = new javax.swing.JComboBox();
        EW_Combo = new javax.swing.JComboBox();
        WE_Combo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        NLON_Text = new javax.swing.JTextField();
        NLAT_Text = new javax.swing.JTextField();
        UpperY_Spin = new javax.swing.JSpinner();
        LowerY_Spin = new javax.swing.JSpinner();
        LeftX_Spin = new javax.swing.JSpinner();
        RightX_Spin = new javax.swing.JSpinner();
        CellSize_Label = new javax.swing.JLabel();
        CellSize_Text = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        DataTypeLabel = new javax.swing.JLabel();
        DataTypeText = new javax.swing.JTextField();
        PrecisionLabel = new javax.swing.JLabel();
        PrecisionText = new javax.swing.JTextField();
        MinimumLabel = new javax.swing.JLabel();
        MinimumText = new javax.swing.JTextField();
        MaximumLabel = new javax.swing.JLabel();
        MaximumText = new javax.swing.JTextField();
        SizeInBytesLabel = new javax.swing.JLabel();
        SizeInBytesText = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        ResolutionText = new javax.swing.JTextField();
        OkButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ETOPO Gridded Data");
        setName("MapDialog"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 10));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Grid"));
        jPanel1.setPreferredSize(new java.awt.Dimension(220, 240));
        jPanel1.setLayout(null);

        NS_Combo.setPreferredSize(new java.awt.Dimension(42, 24));
        NS_Combo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                NS_ComboItemStateChanged(evt);
            }
        });
        jPanel1.add(NS_Combo);
        NS_Combo.setBounds(110, 20, 42, 24);

        SN_Combo.setPreferredSize(new java.awt.Dimension(42, 24));
        SN_Combo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                SN_ComboItemStateChanged(evt);
            }
        });
        jPanel1.add(SN_Combo);
        SN_Combo.setBounds(110, 100, 42, 24);

        EW_Combo.setPreferredSize(new java.awt.Dimension(42, 24));
        EW_Combo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                EW_ComboItemStateChanged(evt);
            }
        });
        jPanel1.add(EW_Combo);
        EW_Combo.setBounds(60, 60, 42, 24);

        WE_Combo.setPreferredSize(new java.awt.Dimension(42, 24));
        WE_Combo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                WE_ComboItemStateChanged(evt);
            }
        });
        jPanel1.add(WE_Combo);
        WE_Combo.setBounds(170, 60, 42, 24);

        jLabel5.setText("Number of cells (LON x LAT)");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(20, 170, 180, 14);

        NLON_Text.setEditable(false);
        NLON_Text.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        NLON_Text.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel1.add(NLON_Text);
        NLON_Text.setBounds(40, 200, 60, 24);

        NLAT_Text.setEditable(false);
        NLAT_Text.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        NLAT_Text.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel1.add(NLAT_Text);
        NLAT_Text.setBounds(120, 200, 60, 24);

        UpperY_Spin.setPreferredSize(new java.awt.Dimension(50, 24));
        UpperY_Spin.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                UpperY_SpinStateChanged(evt);
            }
        });
        jPanel1.add(UpperY_Spin);
        UpperY_Spin.setBounds(60, 20, 50, 24);

        LowerY_Spin.setPreferredSize(new java.awt.Dimension(50, 24));
        LowerY_Spin.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LowerY_SpinStateChanged(evt);
            }
        });
        jPanel1.add(LowerY_Spin);
        LowerY_Spin.setBounds(60, 100, 50, 24);

        LeftX_Spin.setPreferredSize(new java.awt.Dimension(50, 24));
        LeftX_Spin.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LeftX_SpinStateChanged(evt);
            }
        });
        jPanel1.add(LeftX_Spin);
        LeftX_Spin.setBounds(10, 60, 50, 24);

        RightX_Spin.setPreferredSize(new java.awt.Dimension(50, 24));
        RightX_Spin.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                RightX_SpinStateChanged(evt);
            }
        });
        jPanel1.add(RightX_Spin);
        RightX_Spin.setBounds(120, 60, 50, 24);

        CellSize_Label.setText("Cell size");
        CellSize_Label.setMaximumSize(new java.awt.Dimension(100, 30));
        CellSize_Label.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel1.add(CellSize_Label);
        CellSize_Label.setBounds(20, 140, 60, 24);

        CellSize_Text.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        CellSize_Text.setEnabled(false);
        CellSize_Text.setPreferredSize(new java.awt.Dimension(85, 24));
        jPanel1.add(CellSize_Text);
        CellSize_Text.setBounds(80, 140, 100, 24);

        getContentPane().add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Data"));
        jPanel2.setPreferredSize(new java.awt.Dimension(180, 240));
        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 15));

        DataTypeLabel.setText("Data type");
        DataTypeLabel.setMaximumSize(new java.awt.Dimension(100, 30));
        DataTypeLabel.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel2.add(DataTypeLabel);

        DataTypeText.setFont(new java.awt.Font("MS Sans Serif", 1, 13)); // NOI18N
        DataTypeText.setEnabled(false);
        DataTypeText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel2.add(DataTypeText);

        PrecisionLabel.setText("Precision");
        PrecisionLabel.setMaximumSize(new java.awt.Dimension(100, 30));
        PrecisionLabel.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel2.add(PrecisionLabel);

        PrecisionText.setFont(new java.awt.Font("MS Sans Serif", 1, 13));
        PrecisionText.setEnabled(false);
        PrecisionText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel2.add(PrecisionText);

        MinimumLabel.setText("Minimum");
        MinimumLabel.setMaximumSize(new java.awt.Dimension(100, 30));
        MinimumLabel.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel2.add(MinimumLabel);

        MinimumText.setFont(new java.awt.Font("MS Sans Serif", 1, 13)); // NOI18N
        MinimumText.setEnabled(false);
        MinimumText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel2.add(MinimumText);

        MaximumLabel.setText("Maximum");
        MaximumLabel.setMaximumSize(new java.awt.Dimension(100, 30));
        MaximumLabel.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel2.add(MaximumLabel);

        MaximumText.setFont(new java.awt.Font("MS Sans Serif", 1, 13)); // NOI18N
        MaximumText.setEnabled(false);
        MaximumText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel2.add(MaximumText);

        SizeInBytesLabel.setText("Size in bytes");
        SizeInBytesLabel.setMaximumSize(new java.awt.Dimension(100, 30));
        SizeInBytesLabel.setPreferredSize(new java.awt.Dimension(70, 24));
        jPanel2.add(SizeInBytesLabel);

        SizeInBytesText.setFont(new java.awt.Font("MS Sans Serif", 1, 13)); // NOI18N
        SizeInBytesText.setEnabled(false);
        SizeInBytesText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel2.add(SizeInBytesText);

        getContentPane().add(jPanel2);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setPreferredSize(new java.awt.Dimension(405, 40));
        jPanel3.setLayout(null);

        jLabel6.setFont(new java.awt.Font("MS Sans Serif", 2, 13));
        jLabel6.setText("Enter coarser resolution (in minutes) if needed");
        jLabel6.setPreferredSize(new java.awt.Dimension(280, 24));
        jPanel3.add(jLabel6);
        jLabel6.setBounds(10, 10, 280, 24);

        ResolutionText.setText("0");
        ResolutionText.setPreferredSize(new java.awt.Dimension(75, 24));
        jPanel3.add(ResolutionText);
        ResolutionText.setBounds(315, 10, 80, 24);

        getContentPane().add(jPanel3);

        OkButton.setAlignmentX(0.5F);
        OkButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        OkButton.setLabel("OK");
        OkButton.setMaximumSize(new java.awt.Dimension(75, 27));
        OkButton.setMinimumSize(new java.awt.Dimension(75, 20));
        OkButton.setPreferredSize(new java.awt.Dimension(75, 24));
        OkButton.setSelected(true);
        OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkButtonActionPerformed(evt);
            }
        });
        getContentPane().add(OkButton);

        CancelButton.setText("Cancel");
        CancelButton.setAlignmentX(0.5F);
        CancelButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        CancelButton.setMinimumSize(new java.awt.Dimension(75, 20));
        CancelButton.setPreferredSize(new java.awt.Dimension(75, 24));
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(CancelButton);

        getAccessibleContext().setAccessibleName("Geodas File Contents");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void WE_ComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_WE_ComboItemStateChanged
        if (shown) {
            if (WE_Combo.getSelectedItem().toString() == "W"){
                Right_x = -Float.parseFloat(RightX_Spin.getValue().toString());
                if (EW_Combo.getSelectedItem().toString() == "E") CaseLon = 1; 
                else CaseLon = 3;
            }
            else if (WE_Combo.getSelectedItem().toString() == "E"){
                Right_x =  Float.parseFloat(RightX_Spin.getValue().toString());
                CaseLon = 2; 
            }
            UpdateLongitude();
        }
    }//GEN-LAST:event_WE_ComboItemStateChanged

    private void EW_ComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_EW_ComboItemStateChanged
        if (shown){
            if (EW_Combo.getSelectedItem().toString() == "E"){
                Left_x =  Float.parseFloat(LeftX_Spin.getValue().toString());
                if (WE_Combo.getSelectedItem().toString() == "W") CaseLon = 1; 
                else CaseLon = 2;
            }
            else if (EW_Combo.getSelectedItem().toString() == "W"){
                Left_x = -Float.parseFloat(LeftX_Spin.getValue().toString());
                if (WE_Combo.getSelectedItem().toString() == "E") CaseLon = 2;
                else CaseLon = 3;
            }
            UpdateLongitude();
        }
    }//GEN-LAST:event_EW_ComboItemStateChanged

    private void SN_ComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_SN_ComboItemStateChanged
        if (shown){
            if (SN_Combo.getSelectedItem().toString() == "S"){
                Lower_y = -Float.parseFloat(LowerY_Spin.getValue().toString());
                if (NS_Combo.getSelectedItem().toString() == "N") CaseLat = 1;
                else CaseLat = 2;
            }
            else if (SN_Combo.getSelectedItem().toString() == "N"){
                Lower_y =  Float.parseFloat(LowerY_Spin.getValue().toString());
                if (NS_Combo.getSelectedItem().toString() == "N") CaseLat = 1;
                else CaseLat = 3;
            }
            UpdateLattitude();
        }
    }//GEN-LAST:event_SN_ComboItemStateChanged

    private void NS_ComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_NS_ComboItemStateChanged
        if (shown){
            if (NS_Combo.getSelectedItem().toString() == "N"){
                Upper_y =  Float.parseFloat(UpperY_Spin.getValue().toString());
                CaseLat = 1;
            }
            else if (NS_Combo.getSelectedItem().toString() == "S"){
                Upper_y = -Float.parseFloat(UpperY_Spin.getValue().toString());
                if (SN_Combo.getSelectedItem().toString() == "S") CaseLat = 2;
                else CaseLat = 3;
            }
            UpdateLattitude();
        }
    }//GEN-LAST:event_NS_ComboItemStateChanged

    private void LeftX_SpinStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LeftX_SpinStateChanged
        EW_ComboItemStateChanged(null);
        //LeftX_Spin.getModel().setValue(new Float(Math.abs(Left_x)));
    }//GEN-LAST:event_LeftX_SpinStateChanged

    private void LowerY_SpinStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LowerY_SpinStateChanged
        SN_ComboItemStateChanged(null);
    }//GEN-LAST:event_LowerY_SpinStateChanged

    private void RightX_SpinStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_RightX_SpinStateChanged
        WE_ComboItemStateChanged(null);
    }//GEN-LAST:event_RightX_SpinStateChanged

    private void UpperY_SpinStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_UpperY_SpinStateChanged
        NS_ComboItemStateChanged(null);
    }//GEN-LAST:event_UpperY_SpinStateChanged

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        doClose();
        OnOK = false;
    }//GEN-LAST:event_CancelButtonActionPerformed

    private boolean CheckValues(){
        boolean answer = true;
        float val;
        try{
            val = Float.parseFloat(ResolutionText.getText());
        } catch (NumberFormatException e){return false;}
        if (val<=0) answer = false;
        return answer;
    }
    
    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed
        if ((NLAT_Text.getText().compareTo(" ") == 0)||(NLON_Text.getText().compareTo(" ") == 0)){
            if (err_text == ""){
                err_text = "Improper domain has been chosen!";
            }
            ErrorMessage();
        } else if (!CheckValues()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else if (!CheckDomain()){
            err_text = "Improper domain has been chosen!";
            ErrorMessage();
        } else {
            NewResolution = Float.parseFloat(ResolutionText.getText());
            doClose();
            OnOK = true;
        }
    }//GEN-LAST:event_OkButtonActionPerformed

    private void UpdateLattitude(){
        switch (CaseLat){
            case 1:
                if (Upper_y >= Lower_y){
                    Nlat = (int)((Upper_y - Lower_y)*3600/CellSize)+1;
                    NLAT_Text.setText(String.valueOf(Nlat));
                } else {
                    NLAT_Text.setText(" "); 
                    err_text = "Upper lattitude must be greater than lower lattitude";
                }
                break;
            case 2:
                if (Math.abs(Lower_y) >= Math.abs(Upper_y)){
                    Nlat = (int)((-Lower_y + Upper_y)*3600/CellSize)+1;
                    NLAT_Text.setText(String.valueOf(Nlat));
                }else {
                    NLAT_Text.setText(" "); 
                    err_text = "Lower lattitude must be greater than upper lattitude";
                }
                break;
            case 3:
                NLAT_Text.setText(" "); 
                err_text = "Upper lattitude must be greater than lower lattitude";
                break;
        }
        if ((Math.abs(Lower_y) > 90) || (Math.abs(Upper_y)>90)){
            NLAT_Text.setText(" "); 
            err_text = "Lattitude value cannot be greater than 90";
        }
      }
    
    private void UpdateLongitude(){
        two_parts_domain = false;
        switch (CaseLon){
            case 1:
                Nlon = (int)((360+Right_x-Left_x)*3600/CellSize)+1;
                NLON_Text.setText(String.valueOf(Nlon));
                break;
            case 2:
                if (Right_x > Left_x){
                    Nlon = (int)((Right_x - Left_x)*3600/CellSize)+1;
                    if ((Nlon_init==(int)(360*3600/CellSize)+1)){
                        two_parts_domain = true;
                        float x = Left_x; 
                        float y = Right_x;
                        if (x<0) x = 360+x; if (y<0) y = 360+y;
                        Nlon = (int)((360-x+y)*3600/CellSize)+1;
                    }
                    NLON_Text.setText(String.valueOf(Nlon));
                } else if ((Right_x <= Left_x)&&(Nlon_init==(int)(360*3600/CellSize)+1)){
                    two_parts_domain = true;
                    float x = Left_x; 
                    float y = Right_x;
                    if (x<0) x = 360+x; if (y<0) y = 360+y;
                    Nlon = (int)((360-x+y)*3600/CellSize)+1;
                    NLON_Text.setText(String.valueOf(Nlon));
                }
                else {
                    NLON_Text.setText(" "); 
                    err_text = "Right longitude value must be greater than left longitude";
                }
                break;
            case 3:
                if (Left_x < Right_x){
                    Nlon = (int)((Right_x-Left_x)*3600/CellSize)+1;
                    NLON_Text.setText(String.valueOf(Nlon));
                } else if ((Left_x >= Right_x)&&(Nlon_init==(int)(360*3600/CellSize)+1)){
                    two_parts_domain = true;
                    float x = Left_x; 
                    float y = Right_x;
                    if (x<0) x = 360+x; if (y<0) y = 360+y;
                    Nlon = (int)((360-x+y)*3600/CellSize)+1;
                    NLON_Text.setText(String.valueOf(Nlon));
                }else {
                    NLON_Text.setText(" ");
                    err_text = "Left western longitude value must be greater than right longitude";
                }
                break;
        }
        if ((Math.abs(Left_x) > 180) || (Math.abs(Right_x)>180)){
            NLON_Text.setText(" "); 
            err_text = "Longitude value cannot be greater than 180";
        }
   }
    
    private void ErrorMessage(){
        erd.err_text = err_text;
        erd.show();
    }
    
    public boolean CheckDomain(){
        boolean FlagLon = true;
        boolean FlagLat = true;
        
        // If number of cells is larger than initial
        if ((Nlon > Nlon_init) || (Nlat > Nlat_init))
            return false;
        
        // Check longitude
        if ((LeftX_init >= 0) && (RightX_init > 0)){ // if EE case initially
            if ((Left_x >= LeftX_init) && (Right_x >=0) && (Right_x <= RightX_init)) // EE case only
                FlagLon = true;
            else FlagLon = false;
        } else if ((LeftX_init >= 0) && (RightX_init <= 0)){ // if EW case
            switch (CaseLon){
                case 1:
                    if ((Left_x >=LeftX_init) && (Right_x <= RightX_init))
                        FlagLon = true;
                    else FlagLon = false;
                    break;
                case 2:
                    if (Left_x >= LeftX_init)
                        FlagLon = true;
                    else if ((Left_x < LeftX_init)&& (Nlon_init==(int)(360*3600/CellSize)+1))
                        FlagLon = true;
                    else FlagLon = false;
                    break;
                case 3:
                    if  (Right_x <= RightX_init)
                        FlagLon = true;
                    else if ((Right_x > RightX_init)&&(Nlon_init==(int)(360*3600/CellSize)+1))
                        FlagLon = true;
                    else FlagLon = false;
                    break;
            }
        } else if ((LeftX_init < 0) && (RightX_init < 0)){ // WW initially
            if ((Left_x >= LeftX_init) && (Right_x <= RightX_init) && (Left_x < 0))
                FlagLon = true;
            else FlagLon = false;
        } else if ((LeftX_init < 0) && (Right_x >=0)){     // WE initially
            switch (CaseLon){
                case 1:
                    FlagLon = false;
                    break;
                case 2:
                    if ((Left_x >= LeftX_init) && (Right_x <= RightX_init))
                        FlagLon = true;
                    else FlagLon = false;
                    break;
                case 3:
                    if (Left_x >= LeftX_init)
                        FlagLon = true;
                    else FlagLon = false;
                    break;
            }
        }
        
        // Check lattitude 
        if ((UpperY_init >= 0) && (LowerY_init >= 0)){ // if NN case initially
            if ((Upper_y <= UpperY_init) && (Upper_y >= 0) && (Lower_y >= LowerY_init)) // NN case only
                FlagLat = true;
            else FlagLat = false;
        } else if ((UpperY_init >= 0) && (LowerY_init < 0)){ // if NS case
            switch (CaseLat){
                case 1:
                    if ((Upper_y <= UpperY_init) && (Lower_y >= LowerY_init))
                        FlagLat = true;
                    else FlagLat = false;
                    break;
                case 2:
                    if (Lower_y >= LowerY_init)
                        FlagLat = true;
                    else FlagLat = false;
                    break;
            }
        } else if ((UpperY_init < 0) && (LowerY_init < 0)){ // SS initially
            if ((Upper_y <= UpperY_init) && (Lower_y >= LowerY_init) && (Lower_y < 0))
                FlagLat = true;
            else FlagLat = false;
        }
        return (FlagLon && FlagLat);
    }
    
    public void doClose(){
        setVisible(false);
        dispose();
    }
    
    public void OnShown(){
        formComponentShown(null);
    }
    
    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        getRootPane().setDefaultButton(OkButton);
        if (!NotForChange){
            Left_x = LeftX_init;
            Right_x = RightX_init;
            Upper_y = UpperY_init;
            Lower_y = LowerY_init;
            Nlon    = Nlon_init;
            Nlat    = Nlat_init;
        }
        Object[] Value = new Object[] {new Float(Math.abs(Left_x)), new Float(Math.abs(Upper_y)), 
                                       new Float(Math.abs(Right_x)), new Float(Math.abs(Lower_y))};
       
        //model1 = new javax.swing.SpinnerNumberModel(0, 0, 180, 0.5);
        LeftX_Spin.setModel(modelEW);
        RightX_Spin.setModel(modelWE);
        UpperY_Spin.setModel(modelNS);
        LowerY_Spin.setModel(modelSN);                                       
        if (Upper_y >= 0)
            NS_Combo.setSelectedItem(Segment[0]);
        else 
            NS_Combo.setSelectedItem(Segment[1]);
        if (Left_x >= 0)
            EW_Combo.setSelectedItem(Segment[2]);
        else 
            EW_Combo.setSelectedItem(Segment[3]);
        if (Lower_y < 0)
            SN_Combo.setSelectedItem(Segment[1]);
        else 
            SN_Combo.setSelectedItem(Segment[0]);
        if (Right_x <= 0)
            WE_Combo.setSelectedItem(Segment[3]);
        else 
            WE_Combo.setSelectedItem(Segment[2]);
        LeftX_Spin.setValue(Value[0]);
        RightX_Spin.setValue(Value[2]);
        UpperY_Spin.setValue(Value[1]);
        LowerY_Spin.setValue(Value[3]);
        if (DataType == 1) 
            DataTypeText.setText("depth");
        else if (DataType == 2)
            DataTypeText.setText("density");
        else if (DataType == 3)
            DataTypeText.setText("radius");
        if (Precision == 1)
            PrecisionText.setText("meters");
        else if (Precision == 10)
            PrecisionText.setText("tenth meters");
        SizeInBytesText.setText(String.valueOf(Nbytes));
        MinimumText.setText(String.valueOf(MINval));
        MaximumText.setText(String.valueOf(MAXval));
        String str = String.valueOf(CellSize/60)+" minutes";
        CellSize_Text.setText(str);
        ResolutionText.setText(String.valueOf(CellSize/60));
        NLON_Text.setText(String.valueOf(Nlon));
        NLAT_Text.setText(String.valueOf(Nlat));
        shown = true;
        NotForChange = false;
    }//GEN-LAST:event_formComponentShown
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel CellSize_Label;
    private javax.swing.JTextField CellSize_Text;
    private javax.swing.JLabel DataTypeLabel;
    private javax.swing.JTextField DataTypeText;
    private javax.swing.JComboBox EW_Combo;
    private javax.swing.JSpinner LeftX_Spin;
    private javax.swing.JSpinner LowerY_Spin;
    private javax.swing.JLabel MaximumLabel;
    private javax.swing.JTextField MaximumText;
    private javax.swing.JLabel MinimumLabel;
    private javax.swing.JTextField MinimumText;
    private javax.swing.JTextField NLAT_Text;
    private javax.swing.JTextField NLON_Text;
    private javax.swing.JComboBox NS_Combo;
    private javax.swing.JButton OkButton;
    private javax.swing.JLabel PrecisionLabel;
    private javax.swing.JTextField PrecisionText;
    private javax.swing.JTextField ResolutionText;
    private javax.swing.JSpinner RightX_Spin;
    private javax.swing.JComboBox SN_Combo;
    private javax.swing.JLabel SizeInBytesLabel;
    private javax.swing.JTextField SizeInBytesText;
    private javax.swing.JSpinner UpperY_Spin;
    private javax.swing.JComboBox WE_Combo;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
    
}
